﻿using System;
using wojilu.Apps.Qycms.Domain;
using wojilu.Web.Mvc.Attr;

namespace wojilu.Web.Controller.Cn.Form
{
    public class ListController : BaseTempController
    {
        public ListController()
        {
            LayoutControllerType = typeof(CnController);
        }

        [NonVisit]
        public virtual void setpublic()
        {
            set("top", loadHtml(new Cn.PublicController().Top));
            set("bottom", loadHtml(new Cn.PublicController().Bottom));
        }

        [HttpGet]
        public virtual void Show(Int64 Id)
        {
            var f = Form_Group.findById(Id);
            var template = String.Empty;
            if (!f.template.StrIsNullOrEmpty())
            {
                template = f.template;
            }
            view(DefaultTemp + "Form/" + template);

            //标题、关键词、描述
            var Title = String.Empty;
            var Keywords = String.Empty;
            var Description = String.Empty;
            var Position = String.Empty;

            Title = f.formgroupname + "-" + YBBCommon.GetLangStr(CurrentLng, "sitename");
            Keywords = YBBCommon.GetLangStr(CurrentLng, "keyword");
            Description = YBBCommon.GetLangStr(CurrentLng, "description");

            ctx.Page.SetTitle(Title);
            ctx.Page.Keywords = Keywords;
            ctx.Page.Description = Description;

            Position += "<span>" + f.formgroupname + "</span>";
            set("daohang", Position);

            bind("form", f);
            var fa = Form_Attr.find(" fgid=" + Id + " order by pid asc,id asc").list();
            bind("formattr", fa);

            var token = ctx.web.SessionGet("CRSFToken");
            if (token == null)
            {
                token = CreateToken();
                ctx.web.SessionSet("CRSFToken", token);
            }
            set("token_key", token);

            setpublic();
            SetPublic();
        }

        #region token
        private string _salt = DateTime.Now.ToString();

        private string CreateToken()
        {
            return new HashTool().GetBySalt(ctx.web.SessionId, _salt, HashType.SHA1);
        }

        private bool TokenIsOK(string token)
        {
            if (ctx.web.SessionGet("CRSFToken") == null)
            {
                return false;
            }
            string tokenInServer = ctx.web.SessionGet("CRSFToken").ToString();
            return tokenInServer == token ? true : false;
        }
        #endregion

    }
}
